/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Objects;

import Resources.RandomGenerator;
import Resources.GameResource;
import Resources.Resource;
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author ThanhTrung
 */
public class Bottle {

    private int screen_x;
    private int screen_y;
    private int speed;
    private int w;
    private int h;
    private static final int PX_PER_STEP = 3;
    RandomGenerator rand;
    private Image bottle;
    private Image bg_play;

    public Bottle() throws Exception {
        bottle = Resource.getImage(GameResource.img, GameResource.IMG_BOTTLE);
        bg_play = Resource.getImage(GameResource.img, GameResource.IMG_BACKGROUND);
        rand = new RandomGenerator();
        this.w = bottle.getWidth();
        this.h = bottle.getHeight();
        this.speed = rand.random(new Random(), 1, 4);
        initXY();
    }

    public void initXY() throws Exception {
        this.screen_x = rand.random(new Random(), 5, bg_play.getWidth() - 5);
        this.screen_y = rand.random(new Random(), -200, -30);
    }

    public int getScreen_x() {
        return screen_x;
    }

    public void setScreen_x(int screen_x) {
        this.screen_x = screen_x;
    }

    public int getScreen_y() {
        return screen_y;
    }

    public void setScreen_y(int screen_y) {
        this.screen_y = screen_y;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    public RandomGenerator getRand() {
        return rand;
    }

    public void setRand(RandomGenerator rand) {
        this.rand = rand;
    }

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public void update() {
        screen_y += speed;
    }

    public void paint(Graphics g) {
        g.drawImage(bottle, screen_x, screen_y, Graphics.TOP | Graphics.LEFT);
    }

    public boolean checkCollided(Bariel b) throws Exception {
        screen_y = screen_y + speed;
        if (screen_x + w >= b.getScreen_x()
                && screen_x <= (b.getScreen_x() + b.getW())
                && screen_y + h >= b.getScreen_y()
                && screen_y + h <= b.getScreen_y() + b.getH() / 2) {
            initXY();
            return true;
        }
        return false;
    }

    public boolean checkFail() throws Exception {
        if (screen_y >= bg_play.getHeight() - 4) {
            initXY();
            return false;
        }
        return true;
    }
    
}
